package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"product666/appconfig"
)

var (
	DB  *gorm.DB
	err error
)

func MysqlInit() {
	var appConf = appconfig.Conf.MysqlConfig
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", appConf.User, appConf.Password, appConf.Hort, appConf.Post, appConf.Data)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("数据库连接失败")
	} else {
		fmt.Println("数据库连接成功")
	}
	err = DB.AutoMigrate(&User{}, &Product{})
	if err != nil {
		fmt.Println("数据库迁移失败")
	} else {
		fmt.Println("数据库迁移成功")
	}

}
